package com.xizi.秋招笔试;

import java.util.Scanner;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-8-8上午 10:54
 */
public class E {

    static class Node {
        int data;
        Node left;
        Node right;

        Node(int data) {
            this.data = data;
            left = null;
            right = null;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "data=" + data +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        Node root1 = new Node(1);
        int index = 1;
        while (index <= n) {
            Node root = getNode(index, root1);
            if (root == null) {
                root = new Node(index);
//                System.out.println(root.toString());
            }
            if (index == k) {
                root1 = root;
            }
            int left = sc.nextInt();
            int right = sc.nextInt();
            if (left == 0) {
                root.left = null;
            } else {
                root.left = new Node(left);
            }
            if (right == 0) {
                root.right = null;
            } else {
                root.right = new Node(right);
            }
            index++;

        }

        while (m-- > 0) {
            int num = sc.nextInt();
//            Node node = getNode(num, root1);
//            Node temp=node.left;
//            node.right=null;
//            node.right=node.left;
//            node.left=null;
//            node.left=temp;
        }
        inorder(root1);

    }

    public static Node getNode(int num, Node root) {
        if (root == null) {
            return null;
        }
        if (root.left != null) getNode(num, root.left);
        if (root.data == num) {
            System.out.println(root.toString());
            return root;
        }
        if (root.right != null) getNode(num, root.right);
        return null;
    }

    public static void inorder(Node root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        if (root != null) {
            System.out.print(root.data + " ");
        }
        inorder(root.right);
    }
}
